import React from 'react';
import { App } from 'antd';

type ToastOptions = {
  duration?: number;
  key?: string;
  onClose?: () => void;
};

// 默认配置
const defaultToastOptions = {
  duration: 4,
};

// 获取 message 实例的 hook
const useMessage = () => {
  const { message } = App.useApp();
  return message;
};

// Toast 方法 - 使用 Ant Design 的 message 组件
export const showToast = {
  success: (content: string, options?: ToastOptions) => {
    const { message } = App.useApp();
    return message.success({
      content,
      duration: options?.duration || defaultToastOptions.duration,
      key: options?.key,
      onClose: options?.onClose,
    });
  },
  
  error: (content: string, options?: ToastOptions) => {
    const { message } = App.useApp();
    return message.error({
      content,
      duration: options?.duration || 6, // 错误消息显示更久
      key: options?.key,
      onClose: options?.onClose,
    });
  },
  
  warning: (content: string, options?: ToastOptions) => {
    const { message } = App.useApp();
    return message.warning({
      content,
      duration: options?.duration || defaultToastOptions.duration,
      key: options?.key,
      onClose: options?.onClose,
    });
  },
  
  info: (content: string, options?: ToastOptions) => {
    const { message } = App.useApp();
    return message.info({
      content,
      duration: options?.duration || defaultToastOptions.duration,
      key: options?.key,
      onClose: options?.onClose,
    });
  },
  
  loading: (content: string, options?: ToastOptions) => {
    const { message } = App.useApp();
    return message.loading({
      content,
      duration: options?.duration || 0, // loading 默认不自动关闭
      key: options?.key,
      onClose: options?.onClose,
    });
  },
  
  promise: async <T,>(
    promise: Promise<T>,
    messages: {
      loading: string;
      success: string | ((data: T) => string);
      error: string | ((error: any) => string);
    },
    options?: ToastOptions
  ) => {
    const { message } = App.useApp();
    const key = options?.key || 'promise';
    
    // 显示 loading
    message.loading({
      content: messages.loading,
      key,
      duration: 0,
    });
    
    try {
      const result = await promise;
      const successMsg = typeof messages.success === 'function' 
        ? messages.success(result) 
        : messages.success;
      
      message.success({
        content: successMsg,
        key,
        duration: options?.duration || defaultToastOptions.duration,
      });
      
      return result;
    } catch (error) {
      const errorMsg = typeof messages.error === 'function' 
        ? messages.error(error) 
        : messages.error;
      
      message.error({
        content: errorMsg,
        key,
        duration: options?.duration || 6,
      });
      
      throw error;
    }
  },
  
  dismiss: (key?: string) => {
    const { message } = App.useApp();
    if (key) {
      message.destroy(key);
    } else {
      message.destroy();
    }
  }
};

// 导出 useMessage hook 供组件使用
export { useMessage };